#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os
import sys
import utils


manifests = (
    (
     './static/wap/default.init.js',        
     './static/wap/default.init.min.js'
    ),

    (
     './static/ajax/default.init.js',        
     './static/ajax/default.init.min.js'
    ),

    (
     './static/wap/gallery.init.js',        
     './static/wap/gallery.init.min.js'
    ),

    (
     './static/wap/jplayer.audio.init.js',  
     './static/wap/jplayer.audio.init.min.js'
    ),

    (
     './static/wap/jplayer.audiolist.init.js',  
     './static/wap/jplayer.audiolist.init.min.js'
    ),

    (
     './static/jplayer/add-on/jplayer.playlist.custom.js',  
     './static/jplayer/add-on/jplayer.playlist.custom.min.js',  
    ),
    
    (
     './static/wap/jqm.config.js',  
     './static/wap/jqm.config.min.js'
    ),

    (
     './static/wap/style.css',  
     './static/wap/style.min.css'
    ),

    (
     './static/ajax/style.css',  
     './static/ajax/style.min.css'
    ),

    (
     './static/wap/swipe.init.js',  
     './static/wap/swipe.init.min.js'
    ),

    (
     './static/jqm-icon-pack/font-awesome/jqm-icon-pack-fa.css',  
     './static/jqm-icon-pack/font-awesome/jqm-icon-pack-fa.min.css'
    ),

    (
     './static/jqm-icon-pack/original/jqm-icon-pack-2.0-original.css',  
     './static/jqm-icon-pack/original/jqm-icon-pack-2.0-original.min.css',  
    ),

    (
     './static/jplayer/skins/midnight.black/jplayer.midnight.black.css',  
     './static/jplayer/skins/midnight.black/jplayer.midnight.black.min.css',  
    ),

    (
     './static/swipe/swipe.css',  
     './static/swipe/swipe.min.css',  
    ),

    (
     './static/swipe/swipe.js',  
     './static/swipe/swipe.min.js',  
    ),

    (
     './static/iscroll/iscroll.js',  
     './static/iscroll/iscroll.min.js',  
    ),

    (
     './static/iscroll/jquery.mobile.iscrollview-pull.css',  
     './static/iscroll/jquery.mobile.iscrollview-pull.min.css',  
    ),

    (
     './static/iscroll/jquery.mobile.iscrollview.css',  
     './static/iscroll/jquery.mobile.iscrollview.min.css',  
    ),


    (
     './static/photoswipe/photoswipe.css',  
     './static/photoswipe/photoswipe.min.css',  
    ),

    (
     './static/ajax/render.js',
     './static/ajax/render.min.js',
    ),
    (
     './static/ajax/render.category.js',
     './static/ajax/render.category.min.js',
    ),
    (
     './static/ajax/render.swipeshow.js',
     './static/ajax/render.swipeshow.min.js',
    ),
    (
     './static/ajax/render.listview.js',
     './static/ajax/render.listview.min.js',
    ),
    (
     './static/ajax/render.content.js',
     './static/ajax/render.content.min.js',
    ),
    (
     './static/ajax/news.init.js',
     './static/ajax/news.init.min.js',
    ),
    (
     './static/ajax/newscontent.init.js',
     './static/ajax/newscontent.init.min.js',
    ),
    (
     './static/ajax/gallery.init.js',
     './static/ajax/gallery.init.min.js',
    ),
    (
     './static/ajax/gallerycontent.init.js',
     './static/ajax/gallerycontent.init.min.js',
    ),
    (
     './static/ajax/music.init.js',
     './static/ajax/music.init.min.js',
    ),
    (
     './static/ajax/musiclist.init.js',
     './static/ajax/musiclist.init.min.js',
    ),
    (
     './static/ajax/musicplay.init.js',
     './static/ajax/musicplay.init.min.js',
    ),
    (
     './static/ajax/musiclistplay.init.js',
     './static/ajax/musiclistplay.init.min.js',
    ),
    (
     './static/ajax/video.init.js',
     './static/ajax/video.init.min.js',
    ),
    (
     './static/ajax/videolist.init.js',
     './static/ajax/videolist.init.min.js',
    ),
    (
     './static/ajax/videoplay.init.js',
     './static/ajax/videoplay.init.min.js',
    ),
    (
     './static/ajax/search.init.js',
     './static/ajax/search.init.min.js',
    ),
    (
     './static/ajax/searchresult.init.js',
     './static/ajax/searchresult.init.min.js',
    ),
    (
     './static/ajax/searchresultcontent.init.js',
     './static/ajax/searchresultcontent.init.min.js',
    ),
    (
     './static/ajax/localstorage.js',
     './static/ajax/localstorage.min.js',
    ),
    (
     './static/ajax/swipe.init.js',
     './static/ajax/swipe.init.min.js',
    ),
    (
     './static/ajax/newscontent.init.js',
     './static/ajax/newscontent.init.min.js',
    ),
    (
     './static/ajax/newscomments.init.js',
     './static/ajax/newscomments.init.min.js',
    ),
    (
     './static/ajax/newscomments.content.init.js',
     './static/ajax/newscomments.content.init.min.js',
    ),
    (
     './static/ajax/newscomments.add.init.js',
     './static/ajax/newscomments.add.init.min.js',
    ),
    (
     './static/ajax/more.init.js',
     './static/ajax/more.init.min.js',
    ),
    (
     './static/ajax/clearcache.init.js',
     './static/ajax/clearcache.init.min.js',
    ),
    (
     './static/ajax/map.init.js',
     './static/ajax/map.init.min.js',
    ),
    (
     './static/ajax/contact.init.js',
     './static/ajax/contact.init.min.js',
    ),
    (
     './static/ajax/about.init.js',
     './static/ajax/about.init.min.js',
    ),
    (
     './static/ajax/login.init.js',
     './static/ajax/login.init.min.js',
    ),
    (
     './static/ajax/register.init.js',
     './static/ajax/register.init.min.js',
    ),
    (
     './static/ajax/profile.init.js',
     './static/ajax/profile.init.min.js',
    ),
    (
     './static/ajax/profile.edit.init.js',
     './static/ajax/profile.edit.init.min.js',
    ),
    (
     './static/ajax/device.init.js',
     './static/ajax/device.init.min.js',
    ),
    (
     './static/ajax/jqm.customized.css',
     './static/ajax/jqm.customized.min.css',
    ),
)

def minimize(src, target, dryrun = False):
    cmd = 'java -jar tools/yuicompressor-2.4.8.jar %s' % src
    ret, out = utils.runcmd(cmd)

    print
    print 'FROM: %s ; TO: %s ; RET=%d' % (src, target, ret)
    print

    if not dryrun:
        open(target, 'w').write(out)
    else:
        print out

def check_java():
    cmd = 'java -version'
    ret, out = utils.runcmd(cmd)
    if ret != 0:
        print 'java not installed'
        sys.exit(-1)

if __name__ == '__main__':
    check_java()

    for (src, target) in manifests:
        minimize(src, target, dryrun=False)
